﻿///////////////////////////////////////////////////////////////////////////////
// File:        Thread.hpp
// Author:      671643387@qq.com
// Date:        2016年1月5日 下午5:41:59
// Description:
///////////////////////////////////////////////////////////////////////////////

#ifndef THREAD_THREAD_HPP_
#define THREAD_THREAD_HPP_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <boost/noncopyable.hpp>
#include <fog/Typedef.hpp>

#if defined(__WINDOWS__)
#   define WIN32_LEAN_AND_MEAN
#   include <Windows.h>
#   include <process.h>
#elif defined(__LINUX__)
#   include <pthread.h>
#endif

namespace fog
{
    namespace thread
    {
        class Thread : public boost::noncopyable
        {
        public:
#if defined(__WINDOWS__)
            typedef DWORD       TID_t;
#elif defined(__LINUX__)
            typedef pthread_t   TID_t;
#endif
            enum State
            {
                kNotReady,
                kReady,
                kRunning,
                kExit
            };

        public:
            Thread(void);
            virtual ~Thread(void);

            bool start(void);
            virtual void stop(void) = 0;

            void join(void);

            TID_t id(void) const { return id_; }
            State state(void) const { return state_; }
            static TID_t this_id(void);
            static void sleep(time_t msec);

        protected:
            virtual void run(void) = 0;

        private:
#if defined(__WINDOWS__)
            static unsigned int __stdcall proc_(void* arg);
#elif defined(__LINUX__)
            static void* proc_(void* arg);
#endif

        private:
            TID_t       id_;
            State       state_;
#if defined(__WINDOWS__)
            uintptr_t   handle_;
#endif
        };
    }
}

#endif /* THREAD_THREAD_HPP_ */
